home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 24 / develop issue 24 code / scriptable database 1.0a15.sea / Scriptable Database 1.0a15 / Foundation / ComparisonOperand.cp / ComparisonOperand.cp
Encoding:
Text File  |  1996-02-20  |  16.2 KB  |  374 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ComparisonOperand.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Andy Nicholas, Greg Anderson, Chris Bingham, John Rohrlich, Max McFarland, Paul Ossenbruggen, Mike Kobb
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.          <4>     9/26/95    pco        
  11.  
  12. */
  13.  
  14.  
  15. #include "ComparisonOperand.h"
  16. #include "AbstractScriptableObject.h"
  17. #include "ResolveObjectSpecifier.h"
  18.  
  19. //========================================================================================
  20. //    CLASS TComparisonOperand
  21. //========================================================================================
  22.  
  23. //----------------------------------------------------------------------------------------
  24. // TComparisonOperand::TComparisonOperand
  25. //----------------------------------------------------------------------------------------
  26. TComparisonOperand::TComparisonOperand()
  27.     {
  28.     } // TComparisonOperand::TComparisonOperand
  29.  
  30. //----------------------------------------------------------------------------------------
  31. // TComparisonOperand::~TComparisonOperand
  32. //----------------------------------------------------------------------------------------
  33. TComparisonOperand::~TComparisonOperand()
  34.     {
  35.     } // TComparisonOperand::~TComparisonOperand
  36.     
  37. //----------------------------------------------------------------------------------------
  38. // TComparisonOperand::DisposeOperandData
  39. //
  40. // Comparison operands that do not cache the data they return must dispose of said data
  41. // when it is passed back in to this routine (however, most comparison operands SHOULD
  42. // cache the data and dispose it only when the operand class is disposed).
  43. //----------------------------------------------------------------------------------------
  44. void TComparisonOperand::DisposeOperandData(TDescriptor /*operandData*/) const
  45.     {
  46.     } // TComparisonOperand::DisposeOperandData
  47.     
  48. //----------------------------------------------------------------------------------------
  49. // TComparisonOperand::Compare
  50. //----------------------------------------------------------------------------------------
  51. Boolean TComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const TComparisonOperand& compareWith, TAbstractScriptableObject* objectBeingExamined) const
  52.     {
  53.     DescType myBestType = this->BestType(t, objectBeingExamined);
  54.     DescType otherBestType = compareWith.BestType(t, objectBeingExamined);
  55.     DescType dataTypeToUse = myBestType;
  56.     
  57.     //
  58.     // We will use the best data type for this comparison operand
  59.     // UNLESS the operand we are comparing with cannot return that
  60.     // data type and we can return its data type, in which case
  61.     // we will use its best type instead.
  62.     //
  63.     if((compareWith.CanReturnDataOfType(t, myBestType, objectBeingExamined) == false) && (this->CanReturnDataOfType(t, otherBestType, objectBeingExamined) == true))
  64.         dataTypeToUse = otherBestType;
  65.     
  66.     //
  67.     // Get the data from the respective comparison operands and
  68.     // do the compare
  69.     //
  70.     TDescriptor myOperandData = this->GetData(t, dataTypeToUse, objectBeingExamined);
  71.     TDescriptor otherOperandData = compareWith.GetData(t, dataTypeToUse, objectBeingExamined);
  72.  
  73.     Boolean result = myOperandData.Compare(comparisonOperator, otherOperandData);
  74.     
  75.     //
  76.     // Dispose the operand data if it is no longer needed.
  77.     //
  78.     this->DisposeOperandData(myOperandData);
  79.     compareWith.DisposeOperandData(otherOperandData);
  80.     
  81.     return result;
  82.     } // TComparisonOperand::Compare
  83.     
  84. //----------------------------------------------------------------------------------------
  85. // TComparisonOperand::Compare
  86. //----------------------------------------------------------------------------------------
  87. Boolean TComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const DescType dataTypeToUse, TDescriptor otherOperandData, TAbstractScriptableObject* objectBeingExamined) const
  88.     {    
  89.     //
  90.     // Get the data from the respective comparison operands and
  91.     // do the compare
  92.     //
  93.     TDescriptor myOperandData = this->GetData(t, dataTypeToUse, objectBeingExamined);
  94.     Boolean result = myOperandData.Compare(comparisonOperator, otherOperandData);
  95.     
  96.     //
  97.     // Dispose the operand data if it is no longer needed.
  98.     //
  99.     this->DisposeOperandData(myOperandData);
  100.     
  101.     return result;
  102.     } // TComparisonOperand::Compare
  103.  
  104. //----------------------------------------------------------------------------------------
  105. // TComparisonOperand::ComparisonOperandType
  106. //----------------------------------------------------------------------------------------
  107. ComparitorType TComparisonOperand::ComparisonOperandType() const
  108.     {
  109.     return kAbstractComparitor;
  110.     }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. // TComparisonOperand::PropertyToCompare
  114. //----------------------------------------------------------------------------------------
  115. DescType TComparisonOperand::PropertyToCompare() const
  116.     {
  117.     return typeNull;
  118.     }
  119.  
  120. //========================================================================================
  121. //    CLASS TConstantComparisonOperand
  122. //========================================================================================
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // TConstantComparisonOperand::TConstantComparisonOperand
  126. //----------------------------------------------------------------------------------------
  127. TConstantComparisonOperand::TConstantComparisonOperand(TDescriptor constantData, Boolean passOwnershipOfData /* = true*/)
  128.     {
  129.     fConstantData = constantData;
  130.     fDataOwned = passOwnershipOfData;
  131.     }
  132.     
  133. //----------------------------------------------------------------------------------------
  134. // TConstantComparisonOperand::~TConstantComparisonOperand
  135. //----------------------------------------------------------------------------------------
  136. TConstantComparisonOperand::~TConstantComparisonOperand()
  137.     {
  138.     if(fDataOwned)
  139.         fConstantData.Dispose();
  140.     }
  141.     
  142. //----------------------------------------------------------------------------------------
  143. // TConstantComparisonOperand::BestType
  144. //----------------------------------------------------------------------------------------
  145. DescType TConstantComparisonOperand::BestType(const TAETransaction&, TAbstractScriptableObject* /*objectBeingExamined*/) const
  146.     {
  147.     return fConstantData.DescriptorType();
  148.     }
  149.     
  150. //----------------------------------------------------------------------------------------
  151. // TConstantComparisonOperand::CanReturnDataOfType
  152. //----------------------------------------------------------------------------------------
  153. Boolean TConstantComparisonOperand::CanReturnDataOfType(const TAETransaction&, DescType desiredType, TAbstractScriptableObject* /*objectBeingExamined*/) const
  154.     {
  155.     return fConstantData.DescriptorType() == desiredType;
  156.     }
  157.     
  158. //----------------------------------------------------------------------------------------
  159. // TConstantComparisonOperand::GetData
  160. //----------------------------------------------------------------------------------------
  161. TDescriptor TConstantComparisonOperand::GetData(const TAETransaction&, DescType /*requestedType*/, TAbstractScriptableObject* /*objectBeingExamined*/) const
  162.     {
  163.     return fConstantData;
  164.     }
  165.     
  166. //----------------------------------------------------------------------------------------
  167. // TConstantComparisonOperand::SpecifierForOperand
  168. //----------------------------------------------------------------------------------------
  169. TDescriptor TConstantComparisonOperand::SpecifierForOperand()
  170.     {
  171.     TDescriptor result;
  172.     
  173.     result = fConstantData.Clone();
  174.     
  175.     return result;
  176.     } // TConstantComparisonOperand::SpecifierForOperand
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // TConstantComparisonOperand::ComparisonOperandType
  180. //----------------------------------------------------------------------------------------
  181. ComparitorType TConstantComparisonOperand::ComparisonOperandType() const
  182.     {
  183.     return kLiteralComparitor;
  184.     }
  185.  
  186. //========================================================================================
  187. //    CLASS TPropertyComparisonOperand
  188. //========================================================================================
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // TPropertyComparisonOperand::TPropertyComparisonOperand
  192. //----------------------------------------------------------------------------------------
  193. TPropertyComparisonOperand::TPropertyComparisonOperand(DescType propertyToCompare, TAbstractObjectSpecifier* relativeSpecifier, Boolean isRelativeToObjectBeingExamined) :
  194.     fPropertyToCompare(propertyToCompare),
  195.     fRelativeSpecifier(relativeSpecifier),
  196.     fIsRelativeToObjectBeingExamined(isRelativeToObjectBeingExamined),
  197.     fCachedType(typeNull),
  198.     fObjectBeingExaminedForDataCache(nil),
  199.     fCachedResolvedObject(nil),
  200.     fObjectBeingExaminedForResolveCache(nil)
  201.     {
  202.     fCachedData.ClearDescriptor();
  203.     }
  204.  
  205. //----------------------------------------------------------------------------------------
  206. // TPropertyComparisonOperand::~TPropertyComparisonOperand
  207. //----------------------------------------------------------------------------------------
  208. TPropertyComparisonOperand::~TPropertyComparisonOperand()
  209.     {
  210.     delete fRelativeSpecifier;
  211.     fCachedData.Dispose();
  212.     if(fCachedResolvedObject != nil)
  213.         fCachedResolvedObject->DisposeDesignator();
  214.     }
  215.     
  216. //----------------------------------------------------------------------------------------
  217. // TPropertyComparisonOperand::ResolveObjectBeingExamined
  218. //----------------------------------------------------------------------------------------
  219. TAbstractScriptableObject* TPropertyComparisonOperand::ResolveObjectBeingExamined(const TAETransaction& t, TAbstractScriptableObject* objectBeingExamined) const
  220.     {
  221.     //
  222.     // If our relative specifier isn't relative after all, then
  223.     // clear out the 'objectBeingExamined' variable, because it's
  224.     // irrelevant to the results of the resolution, and we don't
  225.     // want to force the cache to be cleared if it changes.
  226.     //
  227.     if(fIsRelativeToObjectBeingExamined == false)
  228.         objectBeingExamined = nil;
  229.     TAbstractScriptableObject* result = nil;
  230.     
  231.     //
  232.     // If we haven't cached anything, or if the object that the
  233.     // cached item is based on changes, then we need to resolve
  234.     // the relative specifier to determine the object to access.
  235.     //
  236.     if((fCachedResolvedObject == nil) || (fObjectBeingExaminedForResolveCache != objectBeingExamined))
  237.         {
  238.         //
  239.         // Clear the old cache, if any
  240.         //
  241.         if(fCachedResolvedObject != nil)
  242.             ((TPropertyComparisonOperand*)this)->fCachedResolvedObject->DisposeDesignator();
  243.         ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForResolveCache = objectBeingExamined;
  244.         
  245.         //
  246.         // If we have a relative specifier, then resolve it.  If
  247.         // we don't have a relative specifier, then use the null
  248.         // container if this property is not relative to the object
  249.         // being examined.  We never cache the object being examined,
  250.         // though, because we don't own it, and we wouldn't want
  251.         // to double-dispose when this object goes away.
  252.         //
  253.         if(fRelativeSpecifier != nil)
  254.             ((TPropertyComparisonOperand*)this)->fCachedResolvedObject = fRelativeSpecifier->ResolveToken(t, objectBeingExamined);
  255.         else if(fIsRelativeToObjectBeingExamined == false)
  256.             ((TPropertyComparisonOperand*)this)->fCachedResolvedObject = GetNullContainer();
  257.         result = fCachedResolvedObject;
  258.         }
  259.     
  260.     //
  261.     // If we are relative to the object being examined, and we
  262.     // don't have a relative specifier, then use the object
  263.     // being examined as the base for our comparison.
  264.     //
  265.     if(result == nil)
  266.         result = objectBeingExamined;
  267.     
  268.     return result;
  269.     } // TPropertyComparisonOperand::ResolveObjectBeingExamined
  270.     
  271. //----------------------------------------------------------------------------------------
  272. // TPropertyComparisonOperand::BestType
  273. //----------------------------------------------------------------------------------------
  274. DescType TPropertyComparisonOperand::BestType(const TAETransaction& t, TAbstractScriptableObject* objectBeingExamined) const
  275.     {
  276.     objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
  277.     
  278.     return objectBeingExamined ? objectBeingExamined->BestType(t, fPropertyToCompare) : typeNull;
  279.     } // TPropertyComparisonOperand::BestType
  280.     
  281. //----------------------------------------------------------------------------------------
  282. // TPropertyComparisonOperand::CanReturnDataOfType
  283. //----------------------------------------------------------------------------------------
  284. Boolean TPropertyComparisonOperand::CanReturnDataOfType(const TAETransaction& t, DescType desiredType, TAbstractScriptableObject* objectBeingExamined) const
  285.     {
  286.     objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
  287.  
  288.     return objectBeingExamined ? objectBeingExamined->CanReturnDataOfType(t, fPropertyToCompare, desiredType) : false;
  289.     } // TPropertyComparisonOperand::CanReturnDataOfType
  290.     
  291. //----------------------------------------------------------------------------------------
  292. // TPropertyComparisonOperand::GetData
  293. //----------------------------------------------------------------------------------------
  294. TDescriptor TPropertyComparisonOperand::GetData(const TAETransaction& t, DescType requestedType, TAbstractScriptableObject* objectBeingExamined) const
  295.     {
  296.     objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
  297.     TDescriptor result;
  298.     
  299.     if(objectBeingExamined != nil)
  300.         {
  301.         if((fObjectBeingExaminedForDataCache != objectBeingExamined) || (fCachedType != requestedType))
  302.             {
  303.             ((TPropertyComparisonOperand*)this)->fCachedData.Dispose();
  304.             ((TPropertyComparisonOperand*)this)->fCachedType = typeNull;
  305.             ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForDataCache = nil;
  306.             }
  307.             
  308.         result = objectBeingExamined->GetDataOfType(t, fPropertyToCompare, requestedType);
  309.         ((TPropertyComparisonOperand*)this)->fCachedData = result;
  310.         ((TPropertyComparisonOperand*)this)->fObjectBeingExaminedForDataCache = objectBeingExamined;
  311.         ((TPropertyComparisonOperand*)this)->fCachedType = requestedType;
  312.         }
  313.     
  314.     return result;
  315.     } // TPropertyComparisonOperand::GetData
  316.     
  317. //----------------------------------------------------------------------------------------
  318. // TPropertyComparisonOperand::Compare
  319. //----------------------------------------------------------------------------------------
  320. Boolean TPropertyComparisonOperand::Compare(const TAETransaction& t, DescType comparisonOperator, const DescType /*dataTypeToUse*/, TDescriptor otherOperandData, TAbstractScriptableObject* objectBeingExamined) const
  321.     {    
  322.     objectBeingExamined = this->ResolveObjectBeingExamined(t, objectBeingExamined);
  323.  
  324.     return objectBeingExamined->CompareProperty(t, fPropertyToCompare, comparisonOperator, otherOperandData);
  325.     } // TPropertyComparisonOperand::Compare
  326.  
  327. //----------------------------------------------------------------------------------------
  328. // TPropertyComparisonOperand::SpecifierForOperand
  329. //----------------------------------------------------------------------------------------
  330. TDescriptor TPropertyComparisonOperand::SpecifierForOperand()
  331.     {
  332.     TDescriptor result;
  333.     TDescriptor nullContainer;
  334.     
  335.     if(fIsRelativeToObjectBeingExamined == false)
  336.         result.MakeObjectSpecifierForProperty(fPropertyToCompare, nullContainer, false);    
  337.     else if(fRelativeSpecifier == nil)
  338.         result.MakeSpecifierForPropertyOfObjectBeingExamined(fPropertyToCompare);
  339.     else
  340.         {
  341.         TDescriptor container = fRelativeSpecifier->BuildObjectSpecifier();
  342.         result.MakeObjectSpecifierForProperty(fPropertyToCompare, container, true);
  343.         }
  344.     
  345.     return result;
  346.     } // TPropertyComparisonOperand::SpecifierForOperand
  347.     
  348. //----------------------------------------------------------------------------------------
  349. // TPropertyComparisonOperand::ComparisonOperandType
  350. //----------------------------------------------------------------------------------------
  351. ComparitorType TPropertyComparisonOperand::ComparisonOperandType() const
  352.     {
  353.     //
  354.     // ◊Script: we should claim to be a kSimplePropertyComparitor if there is a
  355.     // relative specifier that is nothing more than "of object being examined,"
  356.     // and we should claim to be a kLiteralComparitor if the relative specifier
  357.     // does NOT end with "of object being examined."
  358.     //
  359.     if(fRelativeSpecifier == nil)
  360.         return kSimplePropertyComparitor;
  361.     else
  362.         return kCompoundPropertyComparitor;
  363.     }
  364.  
  365. //----------------------------------------------------------------------------------------
  366. // TPropertyComparisonOperand::PropertyToCompare
  367. //----------------------------------------------------------------------------------------
  368. DescType TPropertyComparisonOperand::PropertyToCompare() const
  369.     {
  370.     return fPropertyToCompare;
  371.     }
  372.  
  373.     
  374.